<?php

namespace app\tools;

use Exception;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
use TencentCloud\Ocr\V20181119\OcrClient;

/**
 * 腾讯云应用接口
 * user ：Du
 * Date : 2022/2/23
 * Time : 11:15
 * Class Tencent
 * projects\tools
 */
class TencentCloud
{

    /**
     * 腾讯云平台API秘钥
     *
     * User : Du
     * Date : 2022/2/23
     * Time : 11:58
     * @var string|mixed
     */
    public string $secretId;

    /**
     * 腾讯云平台API秘钥
     *
     * User : Du
     * Date : 2022/2/23
     * Time : 12:00
     * @var string|mixed
     */
    public string $secretKey;

    /**
     * 初始化
     *
     * Tencent constructor.
     */
    public function __construct()
    {
        $this->secretId = env('tx.secretid');
        $this->secretKey = env('tx.secretkey');
    }


    /**
     * 身份证验证
     *
     * User : Du
     * Date : 2022/2/23
     * Time : 12:01
     * @param array $data
     * @return mixed|string
     */
    public function idCard(array $data)
    {
        try {
            $cred = new Credential($this->secretId, $this->secretKey);
            $httpProfile = new HttpProfile();
            $httpProfile->setEndpoint('ocr.tencentcloudapi.com');

            $clientProfile = new ClientProfile();
            $clientProfile->setHttpProfile($httpProfile);
            $client = new OcrClient($cred, "ap-guangzhou", $clientProfile);

            $req = new IDCardOCRRequest();

            $params = array(
                "ImageUrl" => $data['url'],  //图片地址
                "CardSide" => $data['type']  //人像面：FRONT， 国徽面：BACK，也可忽略系统判断
            );
            $req->fromJsonString(arrayToJson($params));
            $resp = $client->IDCardOCR($req);
            return jsonToArray($resp->toJsonString());
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}